package com.time.company.common.gson;


import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * 解析不同bean的Gson实现类
 * Created by Bing on 2017/8/23.
 * E-mail is tx16bing@163.com
 */

public class GsonImpl extends Json {

    private Gson gson = new Gson();

    @Override
    public String toJson(Object src) {
        return gson.toJson(src);
    }

    @Override
    public <T> T toObject(String json, Class<T> cls) {
        return gson.fromJson(json, cls);
    }

    @Override
    public <T> T toObject(byte[] bytes, Class<T> cls) {
        return gson.fromJson(new String(bytes), cls);
    }

    @Override
    public <T> List<T> toList(String json, Class<T> cls) {
        Type type = new TypeToken<ArrayList<T>>(){}.getType();
        List<T> list  = gson.fromJson(json, type);
        return list;
    }
}
